function login() {
    //获取dom
    var user = document.querySelector(".labelbox .user")
    var userErr = document.querySelector(".labelbox .userErr")
    var pw = document.querySelector(".labelbox .pw")
    var pwErr = document.querySelector(".labelbox .pwErr")
    var loginBtn = document.querySelector(".butss .to-login")



    /**
     * 前端格式验证
     */
    user.onblur = function() {
        var userVal = this.value;
        if (/^[0-9]{11}$/.test(userVal)) {
            //有效
            userErr.innerHTML = ''
            this.flag = true;
        } else {
            userErr.innerHTML = '用户名不合法'
            this.flag = false;
        }
    }
    pw.onblur = function() {
        var pwVal = this.value;
        if (/^[a-zA-Z0-9_]{6,8}$/.test(pwVal)) {
            //有效
            pwErr.innerHTML = ''
            this.flag = true;
        } else {
            pwErr.innerHTML = '密码不合法'
            this.flag = false;
        }
    }

    /**
     * 验证账号的有效性
     */

    loginBtn.onclick = function() {
        if (user.flag && pw.flag) {
            this.innerHTML = '正在登录';
            var xhr = new XMLHttpRequest()
            xhr.open("get", `./api/login.php?user=${user.value}&pw=${pw.value}`, true)
            xhr.send()
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var data = JSON.parse(xhr.responseText);
                    if (data.code == 2) {
                        //登录成功 
                        location.href = './index.html'
                        loginBtn.innerHTML = "登录成功"
                        pEl.innerHTML = user.value;
                        mask.classList.remove("active")
                    }
                }
            }
        } else {
            console.log('用户名或密码不合法');
        }
    }

}
login();